package com.yuelan.apartment.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuelan.apartment.domain.ApaNotice;
import com.yuelan.apartment.mapper.ApaNoticeMapper;
import com.yuelan.apartment.service.ApaNoticeService;
import com.yuelan.common.constant.HttpStatus;
import com.yuelan.common.core.page.TableDataInfo;
import com.yuelan.common.utils.DateUtils;
import com.yuelan.common.utils.SecurityUtils;
import com.yuelan.common.utils.uuid.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

/**
 * @description: 公告
 * @Author: ZhaoYi
 * @date: 24-6-29 20:02
 */

@Service
public class ApaNoticeServiceImpl implements ApaNoticeService {

    @Autowired
    private ApaNoticeMapper apaNoticeMapper;


    @Override
    @CacheEvict(value = "noticeList", allEntries = true)
    public int publishNotice(ApaNotice apaNotice) {
        apaNotice.setId(IdWorker.snowflake());
        apaNotice.setUserId(IdWorker.snowflake());
        apaNotice.setCreateBy(SecurityUtils.getUsername());
        apaNotice.setUpdateBy(SecurityUtils.getUsername());
        apaNotice.setCreateTime(DateUtils.getNowDate());
        apaNotice.setUpdateTime(DateUtils.getNowDate());
        return apaNoticeMapper.insert(apaNotice);
    }


    @Override
    public ApaNotice view(Long id) {
        return apaNoticeMapper.selectById(id);
    }


    @Override
    @CachePut(value = "noticeList")
    public TableDataInfo noticeList(int pageNum, int pageSize) {
        LambdaQueryWrapper<ApaNotice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.eq(ApaNotice::getCreateBy, SecurityUtils.getUsername());

        Page<ApaNotice> noticePage = new Page<>(pageNum, pageSize);
        Page<ApaNotice> noticePageData = apaNoticeMapper.selectPage(noticePage , noticeLambdaQueryWrapper);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(noticePageData.getRecords());
        rspData.setTotal(noticePageData.getTotal());
        return rspData;
    }


    @Override
    @CacheEvict(value = "noticeList", allEntries = true)
    public int removeNotice(Long id) {
        return apaNoticeMapper.deleteById(id);
    }


    @Override
    @CacheEvict(value = "noticeList", allEntries = true)
    public int editNotice(ApaNotice apaNotice) {
        LambdaUpdateWrapper<ApaNotice> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApaNotice::getId, apaNotice.getId());
        apaNotice.setCreateTime(DateUtil.date());
        apaNotice.setUpdateTime(DateUtil.date());
        return apaNoticeMapper.update(apaNotice , updateWrapper);
    }

}
